package com.nova;

import com.nova.source.R;
import com.nova.view.NovaProgressDialog;
import com.nova.view.NovaTextDialog;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.Window;
import android.view.WindowManager;

public class BaseActivity extends Activity {

	protected Activity mContext;
	protected Intent mIntent;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		 getWindow().setFlags(
//		 WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//		 WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
		
		 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 保持屏幕不变黑

		ActivityManager.add(this);
		this.mContext = this;
		this.mIntent = getIntent();
	}

	// public void startXActivityForResult(Intent intent, int requestCode) {
	// startActivityForResult(intent, requestCode);
	// if (getParent() != null)
	// getParent().overridePendingTransition(R.anim.push_left_in,
	// R.anim.push_left_out);
	// else
	// overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	// }

	public void startActivity(Intent intent) {
		super.startActivity(intent);
		if (getParent() != null)
			getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		else
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	/**
	 * Acticity锟斤拷锟斤拷时执锟叫的诧拷锟斤拷
	 */
	protected boolean isDestroyed;

	protected void onDestroy() {
		destroy();
		super.onDestroy();

	}

	private void destroy() {
		this.isDestroyed = true;
		
		ActivityManager.remove(this);
		// overridePendingTransition(R.anim.push_left_out, R.anim.push_left_in);
	}

	protected void onPause() {

		//pause();
		super.onPause();
	}

//	private void pause() {
//		overridePendingTransition(R.anim.right_in, R.anim.right_out);
//	}

	public void finish() {
		destroy();
		cancelTextDialog();
		if (this.progressDialog != null)
			this.progressDialog.cancelImmediately();
		if (textDialog!=null) {
			textDialog.cancel();
			textDialog = null;
		}
		super.finish();
	}

	private NovaProgressDialog progressDialog;
	/**
	 * 显示等待框(不同颜色)
	 */
	public void showProgressDialogBuilder(SpannableStringBuilder builder) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setTextBuilder(builder);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	/**
	 * 显示等待框
	 */
	public void showProgressDialog(String text) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	/**
	 * 显示对话框，boolean是否可以取消 
	 */
	public void showProgressDialog(String text, boolean cancelable) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(cancelable);
		this.progressDialog.show();
	}

	public void showProgressDialog(int text) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	public void showProgressDialog(int text, boolean cancelable) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(cancelable);
		this.progressDialog.show();
	}

	/**
	 * 取消等待框
	 */
	public void cancelProgressDialog() {
		try {
			if (this.progressDialog != null) {
				this.progressDialog.setCancelable(false);
				this.progressDialog.cancel();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private NovaTextDialog textDialog;

	/**
	 * 显示文本信息
	 */
	public void showTextDialog(String text) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setText(text);
		this.textDialog.show();
	}

	/**
	 * 显示数字信息
	 */
	public void showTextDialog(int text) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setText(text);
		this.textDialog.show();
	}
	/**
	 * 显示文本信息，显示完毕后关闭Activity
	 */
	public void showTextDialog(String text,Activity activity) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setFinishActivity(activity);
		this.textDialog.setText(text);
		this.textDialog.show();
	}
	/**
	 * 取消文本信息对话框
	 */
	public void cancelTextDialog() {
		if (this.textDialog != null)
			this.textDialog.cancel();
	}

	/**
	 * 16进制转10进制
	 * 
	 * @param hexString
	 * @return
	 */
	public static final String hexStringToString(String hexString) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < hexString.length(); i = i + 2) {

			// System.out.println(hexString.substring(i, i + 2));

			String data = Integer.toString(Integer.parseInt(hexString.substring(i, i + 2), 16));
			if (data.length() == 1) {
				data = "0" + data;
			}

			sb.append(data);
		}
		return sb.toString();
	}

	/**
	 * 10进制转16进制
	 * 
	 * @param hexString
	 * @return
	 */
	public static final String stringToHexString(String data) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < data.length(); i = i + 2) {

			String hexData = Integer.toHexString(Integer.parseInt(data.substring(i, i + 2)));
			if (hexData.length() == 1) {
				hexData = "0" + hexData;
			}
			sb.append(hexData);
		}
		return sb.toString();
	}

}
